package com.joolun.tennis.service.impl;

import java.util.List;
import com.joolun.common.utils.DateUtils;
import com.joolun.tennis.domain.ClubFile;
import com.joolun.tennis.domain.Coach;
import com.joolun.tennis.domain.WxUserClub;
import com.joolun.tennis.mapper.CoachClubMapper;
import com.joolun.tennis.service.IClubFileService;
import com.joolun.tennis.service.ICoachService;
import com.joolun.tennis.service.IWxUserClubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.joolun.tennis.mapper.ClubMapper;
import com.joolun.tennis.domain.Club;
import com.joolun.tennis.service.IClubService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 俱乐部Service业务层处理
 * 
 * @author www.joolun.com
 * @date 2022-03-08
 */
@Service
public class ClubServiceImpl implements IClubService 
{
    @Autowired
    private ClubMapper clubMapper;

    @Autowired
    private IClubFileService clubFileService;

    @Autowired
    private ICoachService coachService;

    @Autowired
    private CoachClubMapper coachClubMapper;

    @Autowired
    private IWxUserClubService wxUserClubService;

    /**
     * 查询俱乐部
     * 
     * @param id 俱乐部ID
     * @return 俱乐部
     */
    @Override
    public Club selectClubById(Long id)
    {
        Club club = clubMapper.selectClubById(id);
        ClubFile clubFile = new ClubFile();
        clubFile.setClubId(id);
        List<ClubFile> files =  clubFileService.selectClubFileList(clubFile);
        List<Coach> coaches = coachService.selectCoachListByClubId(id);
        club.setFiles(files);
        club.setCoachList(coaches);
        return club;
    }

    /**
     * 查询俱乐部列表
     * 
     * @param club 俱乐部
     * @return 俱乐部
     */
    @Override
    public List<Club> selectClubList(Club club)
    {
        return clubMapper.selectClubList(club);
    }

    @Override
    public List<Club> selectClubWxList(Club club) {
        return clubMapper.selectClubWxList(club);
    }

    @Override
    public List<Club> selectCollectClubList(String wxUserId) {
        return clubMapper.selectCollectClubList(wxUserId);
    }

    /**
     * 新增俱乐部
     * 
     * @param club 俱乐部
     * @return 结果
     */
    @Transactional
    @Override
    public int insertClub(Club club)
    {
        //判断名字重复
        Club  temp = new Club();
        temp.setcName(club.getcName());
        List<Club> list = clubMapper.selectClubByObject(temp);
        if(list.size()>0) return -1;
        club.setCreateTime(DateUtils.getNowDate());
        int rows = clubMapper.insertClub(club);
        //插入俱乐部附件表
        if(club.getFiles() != null){
            club.getFiles().forEach(item -> {
                item.setClubId(club.getId());
                clubFileService.insertClubFile(item);
            });
        }
        return rows;
    }

    /**
     * 修改俱乐部
     * 
     * @param club 俱乐部
     * @return 结果
     */
    @Transactional
    @Override
    public int updateClub(Club club)
    {

        if(club.getFiles().size()>0){
              clubFileService.deleteClubFileByClubId(club.getId());

            club.getFiles().forEach(item->{
                    item.setId(null);
                    item.setClubId(club.getId());
                    clubFileService.insertClubFile(item);
//                }
            });
        }
        club.setUpdateTime(DateUtils.getNowDate());
        return clubMapper.updateClub(club);
    }

    /**
     * 批量删除俱乐部
     * 
     * @param ids 需要删除的俱乐部ID
     * @return 结果
     */
    @Override
    public int deleteClubByIds(Long[] ids)
    {
        int row = clubMapper.deleteClubByIds(ids);
        coachClubMapper.deleteCoachClubByclubIds(ids);
        return row;
    }

    /**
     * 删除俱乐部信息
     * 
     * @param id 俱乐部ID
     * @return 结果
     */
    @Override
    public int deleteClubById(Long id)
    {
        return clubMapper.deleteClubById(id);
    }

    @Override
    public int collectClub(WxUserClub wxUserClub) {
        List<WxUserClub> list = wxUserClubService.selectWxUserClubList(wxUserClub);
        if(list.size()>0) return 0;
        return wxUserClubService.insertWxUserClub(wxUserClub);
    }

    @Override
    public int cancelClub(WxUserClub wxUserClub) {
        return wxUserClubService.deleteWxUserClubByClubId(wxUserClub.getClubId());
    }
}
